<?php
/**
 * 限时折扣
 */

namespace app\admin\controller;
use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Promotionxianshi extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/promotionxianshi.lang.php');
    }


    /**
     * 活动列表
     **/
    public function index()
    {
        //自动开启限时折扣
        if (intval(input('param.promotion_allow')) === 1) {
            $config_model = model('config');
            $update_array = array();
            $update_array['promotion_allow'] = 1;
            $config_model->editConfig($update_array);
        }

        $xianshi_model = model('pxianshi');
        $condition = array();
        if (!empty(input('param.xianshi_name'))) {
            $condition['xianshi_name'] = array('like', '%' . input('param.xianshi_name') . '%');
        }
        if (!empty(input('param.store_name'))) {
            $condition['store_name'] = array('like', '%' . input('param.store_name') . '%');
        }
        if (!empty(input('param.state'))) {
            $condition['xianshi_state'] = intval(input('param.state'));
        }
        $xianshi_list = $xianshi_model->getXianshiList($condition, 10, 'xianshi_state desc, xianshi_end_time desc');
        $this->assign('xianshi_list', $xianshi_list);
        $this->assign('show_page', $xianshi_model->page_info->render());
        $this->assign('xianshi_state_array', $xianshi_model->getXianshiStateArray());

        $this->setAdminCurItem('xianshi_list');
        // 输出自营店铺IDS

        $this->assign('filtered', $condition ? 1 : 0); //是否有查询条件
        $this->assign('flippedOwnShopIds', array_flip(model('store')->getOwnShopIds()));
        return $this->fetch();
    }


    //添加折扣
    public function xianshi_add(){
        $xianshiquota_model = model('pxianshiquota');
        $current_xianshi_quota=array('xianshiquota_starttime'=>TIMESTAMP,'xianshiquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月
        $this->assign('current_xianshi_quota', $current_xianshi_quota);
        $this->assign('isPlatformStore', true);
        $brand_array = [
            'xianshi_state' => 1,
        ];
        $this->assign('brand_array', $brand_array);
        //输出导航
        $this->setAdminCurItem('xianshi_add');
        return $this->fetch();
    }

    /**
     * 保存添加的限时折扣活动
     **/
    public function xianshi_save()
    {
        //验证输入
        $xianshi_name = trim(input('post.xianshi_name'));
        $start_time = strtotime(input('post.start_time'));
        $end_time = strtotime(input('post.end_time'));
        // $lower_limit = intval(input('post.lower_limit'));
        $discount_rate = intval(input('post.discount_rate'));
        $platform_share = intval(input('post.platform_share'));
        $proportion_share = intval(input('post.proportion_share'));
        $supplier_share = intval(input('post.supplier_share'));
        $xianshi_state = intval(input('post.status'));

        //承担比例
        if($platform_share + $proportion_share + $supplier_share != 100){
            ds_json_encode(10001,'承担比例错误');
        }

        // if ($lower_limit <= 0) {
        //     $lower_limit = 1;
        // }
        if (empty($xianshi_name)) {
            ds_json_encode(10001,lang('xianshi_name_error'));
        }
        if ($start_time >= $end_time) {
            ds_json_encode(10002,lang('greater_than_start_time'));
        }

        if (!check_platform_store()) {
            //获取当前套餐
            $xianshiquota_model = model('pxianshiquota');
            $current_xianshi_quota = $xianshiquota_model->getXianshiquotaCurrent(session('store_id'));
            if (empty($current_xianshi_quota)) {
                $current_xianshi_quota=array('xianshiquota_starttime'=>TIMESTAMP,'xianshiquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月
//                if(intval(config('promotion_xianshi_price'))!=0){
//                    ds_json_encode(10001,lang('please_buy_package_first'));
//                }else{
//                    $current_xianshi_quota=array('xianshiquota_starttime'=>TIMESTAMP,'xianshiquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月
//                }
            }
            $quota_start_time = intval($current_xianshi_quota['xianshiquota_starttime']);
            $quota_end_time = intval($current_xianshi_quota['xianshiquota_endtime']);


//            if ($start_time < $quota_start_time) {
//                ds_json_encode(10003,sprintf(lang('xianshi_add_start_time_explain'), date('Y-m-d', $current_xianshi_quota['xianshiquota_starttime'])));
//            }
//            if ($end_time > $quota_end_time) {
//                ds_json_encode(10004,sprintf(lang('xianshi_add_end_time_explain'), date('Y-m-d', $current_xianshi_quota['xianshiquota_endtime'])));
//            }
        }
        
        //生成活动
        $pxianshi_model = model('pxianshi');
        $param = array();
        $param['xianshi_name'] = $xianshi_name;
        $param['xianshi_title'] = input('post.xianshi_title');
        $param['xianshi_explain'] = input('post.xianshi_explain');
        $param['xianshiquota_id'] = isset($current_xianshi_quota['xianshiquota_id']) ? $current_xianshi_quota['xianshiquota_id'] : 0;
        $param['xianshi_starttime'] = $start_time;
        $param['xianshi_end_time'] = $end_time;
//        $param['store_id'] = session('store_id');
//        $param['store_name'] = session('store_name');
//        $param['member_id'] = session('member_id');
//        $param['member_name'] = session('member_name');
        // $param['xianshi_lower_limit'] = $lower_limit;
        $param['discount_rate'] = $discount_rate;
        $param['platform_share'] = $platform_share;
        $param['proportion_share'] = $proportion_share;
        $param['supplier_share'] = $supplier_share;
        $param['xianshi_state'] = $xianshi_state;

        $result = $pxianshi_model->addXianshi($param);
        if ($result) {
//            $this->recordSellerlog(lang('add_limited_time_discount_activity') . $xianshi_name . lang('activity_number') . $result);
            // 添加计划任务
            $this->addcron(array('exetime' => $param['xianshi_end_time'], 'exeid' => $result, 'type' => 7), true);
            ds_json_encode(10000,'限时折扣活动添加成功，请您选择要参加该活动的商品，并发布该活动');
        }
        else {
            ds_json_encode(10001,'限时折扣活动添加失败');
        }
    }
    /*
     * 上下架
     */

    public function status()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));

        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }
        $info = db('pxianshi')->where('xianshi_id', $id)->find();
        if ($status == $info['xianshi_state']) {
            ds_json_encode(10001, '操作失败');
        }
        $data = array();
        $data['xianshi_state'] = $status;
        $rs = db('pxianshi')->where('xianshi_id', $id)->data($data)->update();
        if ($rs) {
//            $this->log(lang('ds_del') . lang('admin_voucher_priceadd') . '[ID:' . $id . ']');
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }
    /**
     * 限时折扣活动管理
     **/
    public function xianshi_manage()
    {
        $pxianshi_model = model('pxianshi');
        $xianshigoods_model = model('pxianshigoods');

        $xianshi_id = intval(input('param.xianshi_id'));
        $xianshi_info = $pxianshi_model->getXianshiInfoByID($xianshi_id, session('store_id'));
        if (empty($xianshi_info)) {
            $this->error(lang('param_error'));
        }
        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('brand_list', $brand_list);

        $this->assign('xianshi_info', $xianshi_info);
        $this->assign('editable', true);

        //获取限时折扣商品列表
        $condition = array();
        $condition['xianshi_id'] = $xianshi_id;
        $xianshigoods_list = $xianshigoods_model->getXianshigoodsExtendList($condition);

        $this->assign('xianshi_goods_list', $xianshigoods_list);


        //输出导航
        $this->setAdminCurItem('xianshi_manage');
        return $this->fetch();
    }

    /**
     * 编辑限时折扣活动
     **/
    public function xianshi_edit()
    {
        if (check_platform_store()) {
            $this->assign('isPlatformStore', true);
        }else {
            $this->assign('isPlatformStore', false);
        }
        $pxianshi_model = model('pxianshi');

        $xianshi_info = $pxianshi_model->getXianshiInfoByID(input('param.xianshi_id'));

        if (empty($xianshi_info) ) {
            //$this->error(lang('param_error'));
        }

        $this->assign('xianshi_info', $xianshi_info);

        $brand_array = [
            'xianshi_state' => $xianshi_info['xianshi_state'],
        ];
        $this->assign('brand_array', $brand_array);
        //输出导航
        $this->setAdminCurItem('xianshi_add');
        return $this->fetch('xianshi_add');
    }

    /**
     * 编辑保存限时折扣活动
     **/
    public function xianshi_edit_save()
    {
        $xianshi_id = input('post.xianshi_id');

        $pxianshi_model = model('pxianshi');
        $xianshigoods_model = model('pxianshigoods');

        $xianshi_info = $pxianshi_model->getXianshiInfoByID($xianshi_id, session('store_id'));
        if (empty($xianshi_info)) {
            ds_json_encode(10001,lang('param_error'));
        }
        //验证输入
        $xianshi_name = trim(input('post.xianshi_name'));
        // $lower_limit = intval(input('post.lower_limit'));
        $discount_rate = intval(input('post.discount_rate'));
        $platform_share = intval(input('post.platform_share'));
        $proportion_share = intval(input('post.proportion_share'));
        $supplier_share = intval(input('post.supplier_share'));
        $start_time = strtotime(input('post.start_time'));
        $end_time = strtotime(input('post.end_time'));
        $xianshi_state = intval(input('post.status'));

        //承担比例
        if($platform_share + $proportion_share + $supplier_share != 100){
            ds_json_encode(10001,'承担比例错误');
        }
        // if ($lower_limit <= 0) {
        //     $lower_limit = 1;
        // }
        if (empty($xianshi_name)) {
            ds_json_encode(10002,lang('xianshi_name_error'));
        }

        //生成活动
        $param = array();
        $param['xianshi_name'] = $xianshi_name;
        $param['xianshi_title'] = input('post.xianshi_title');
        $param['xianshi_explain'] = input('post.xianshi_explain');
        // $param['xianshi_lower_limit'] = $lower_limit;
        $param['discount_rate'] = $discount_rate;
        $param['platform_share'] = $platform_share;
        $param['proportion_share'] = $proportion_share;
        $param['supplier_share'] = $supplier_share;
        $param['xianshi_state'] = $xianshi_state;
        $param['xianshi_starttime'] = $start_time;
        $param['xianshi_end_time'] = $end_time;

        $param_goods=array();
        $param_goods['xianshi_name'] = $xianshi_name;
        $param_goods['xianshi_title'] = input('post.xianshi_title');
        $param_goods['xianshi_explain'] = input('post.xianshi_explain');
        // $param_goods['xianshigoods_lower_limit'] = $lower_limit;


        $result = $pxianshi_model->editXianshi($param, array('xianshi_id' => $xianshi_id));
        $result1 = $xianshigoods_model->editXianshigoods($param_goods, array('xianshi_id' => $xianshi_id));

        //将购物车所有商品失效
        $xianshi_info = db('pxianshigoods')->where(['xianshi_id'=>$xianshi_id])->select();
        if(!empty($xianshi_info)){
            foreach ($xianshi_info as $key => $value) {
            $goods_ids[] = $value['goods_id'];
            }
            foreach ($goods_ids as $key => $value) {
                db('cart')->where(['goods_id'=>$value])->update(['status'=>1]);
            }
        }

        if ($result) {
//            $this->recordSellerlog(lang('edit_limited_time_discount_activity') . $xianshi_name . lang('activity_number') . $xianshi_id);
            ds_json_encode(10000,lang('ds_common_op_succ'));
        }
        else {
            ds_json_encode(10000,lang('ds_common_op_succ'));
        }
    }

    /**
     * 限时折扣活动删除
     **/
    public function xianshi_del()
    {
        $xianshi_id = input('param.xianshi_id');

        $pxianshi_model = model('pxianshi');

        $data = array();
        $data['result'] = true;

        $xianshi_info = $pxianshi_model->getXianshiInfoByID($xianshi_id, session('store_id'));
        if (!$xianshi_info) {
            ds_json_encode(10001,lang('param_error'));
        }

        $pxianshi_model = model('pxianshi');
        $result = $pxianshi_model->delXianshi(array('xianshi_id' => $xianshi_id));

        //将购物车所有商品失效
        $xianshi_info = db('pxianshigoods')->where(['xianshi_id'=>$xianshi_id])->select();
        if(!empty($xianshi_info)){
            foreach ($xianshi_info as $key => $value) {
            $goods_ids[] = $value['goods_id'];
            }
            foreach ($goods_ids as $key => $value) {
                db('cart')->where(['goods_id'=>$value])->update(['status'=>1]);
            }
        }

        if ($result) {
//            $this->recordSellerlog(lang('delete_limited_time_discount_activity') . $xianshi_info['xianshi_name'] . lang('activity_number') . $xianshi_id);
            ds_json_encode(10000,lang('ds_common_op_succ'));
        }
        else {
            ds_json_encode(10001,lang('ds_common_op_fail'));
        }
    }

    /**
     * 选择活动商品
     **/
    public function goods_select()
    {
        $goods_model = model('goods');
        $condition = array();
        // 查询已添加的限购商品
        $data = db('pxianshigoods')->select();
        $data_arr = [];
        foreach ($data as $key => $val) {
            $data_arr[] = $val['goods_id'];
        }
        $condition['goods.goods_id'] = ['not in', $data_arr];
//      $condition['goods.store_id'] = session('store_id');
        if (input('param.brand')) {
            $condition['goods.brand_id'] = input('param.brand');
        }
        $condition['goods.goods_name'] = array('like', '%' . input('param.goods_name') . '%');
        $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 10, 'bundling');
        // $condition['goodscommon.goods_name'] = array('like', '%' . input('param.goods_name') . '%');
        // $goods_list = $goods_model->getGoodsListForPromotion($condition, 'y_goods.goods_id,y_goods.goods_commonid,goodscommon.goods_name,goodscommon.goods_image,goodscommon.goods_price', 10, 'bundling');
        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());
        echo $this->fetch();
    }

    /**
     * 限时折扣商品添加
     **/
    public function xianshi_goods_add()
    {
        $goods_id = intval(input('post.goods_id'));

        //判断当前商品是否在其他的折扣里面
        $goods_pan_info = db('goods')->where(['goods_id'=>$goods_id])->find();

        // $xianshi_status = db('pxianshigoods')->where(['goods_commonid'=>$goods_pan_info['goods_commonid']])->select();
        // if(!empty($xianshi_status)){
        //     $data['result'] = false;
        //     $data['message'] = '当前类商品已在其他活动里，请勿重复添加';
        //     echo json_encode($data);
        //     die;
        // }
        
        $xianshi_id = intval(input('post.xianshi_id'));
//        $xianshi_price = floatval(input('post.xianshi_price'));

        //商品id 和折扣id
        //获取商品价格
        $price = db('goods')->where(['goods_id'=>$goods_id])->find();

        //算提成运营商的价格
        $sale_price = '';
        if($price['t_type'] == 2){
            $sale_price = $price['goods_price'] + $price['t_value'];
        }else{
            $sale_price = $price['goods_price'] + ($price['goods_price'] * ($price['t_value']/100));
        }

        //获取折扣的比率
        $bili = db('pxianshi')->where(['xianshi_id'=>$xianshi_id])->find();

        //获取折扣的价格
        $new_price = $price['goods_price'] * ($bili['discount_rate']/100);

        //获取折扣后的价格
        $discount_price = $sale_price - ($sale_price *($bili['discount_rate']/100));

        $goods_model = model('goods');
        $pxianshi_model = model('pxianshi');
        $xianshigoods_model = model('pxianshigoods');

        $data = array();
        $data['result'] = true;

        $goods_info = $goods_model->getGoodsInfoByID($goods_id);
//        if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
//            $data['result'] = false;
//            $data['message'] = lang('param_error');
//            echo json_encode($data);
//            die;
//        }
        if (empty($goods_info)) {
            $data['result'] = false;
            $data['message'] = lang('param_error');
            echo json_encode($data);
            die;
        }
        $xianshi_info = $pxianshi_model->getXianshiInfoByID($xianshi_id, session('store_id'));
        if (!$xianshi_info) {
            $data['result'] = false;
            $data['message'] = lang('param_error');
            echo json_encode($data);
            die;
        }

        //检查商品是否已经参加同时段活动
        $condition = array();
        $condition['xianshigoods_end_time'] = array('gt', $xianshi_info['xianshi_starttime']);
        $condition['goods_id'] = $goods_id;
        $xianshigoods = $xianshigoods_model->getXianshigoodsExtendList($condition);
        if (!empty($xianshigoods)) {
            $data['result'] = false;
            $data['message'] = lang('product_participated_simultaneous_activities');
            echo json_encode($data);
            die;
        }

        //添加到活动商品表
        $param = array();
        $param['xianshi_id'] = $xianshi_info['xianshi_id'];
        $param['xianshi_name'] = $xianshi_info['xianshi_name'];
        $param['xianshi_title'] = $xianshi_info['xianshi_title'];
        $param['xianshi_explain'] = $xianshi_info['xianshi_explain'];
        $param['goods_id'] = $goods_info['goods_id'];
        $param['goods_commonid'] = $goods_info['goods_commonid'];
        $param['store_id'] = $goods_info['store_id'];
        $param['store_name'] = $goods_info['store_name'];
        $param['goods_name'] = $goods_info['goods_name'];
        $param['goods_price'] = $goods_info['goods_price'];
        $param['xianshigoods_price'] = $new_price;
        $param['goods_image'] = $goods_info['goods_image'];
        $param['xianshigoods_starttime'] = $xianshi_info['xianshi_starttime'];
        $param['xianshigoods_end_time'] = $xianshi_info['xianshi_end_time'];
        $param['xianshigoods_lower_limit'] = $xianshi_info['xianshi_lower_limit'];
        $param['sale_price'] = $sale_price;
        $param['discount_price'] = round($discount_price,2);

        $result = array();
        $xianshigoods_info = $xianshigoods_model->addXianshigoods($param);
        if ($xianshigoods_info) {
            //将购物车里的所有商品进行失效操作
            $cart_list = db('cart')->where(['goods_id'=>$goods_id])->select();
            foreach ($cart_list as $key => $value) {
                db('cart')->where(['goods_id'=>$value['goods_id']])->update(['status'=>1]);
            }

            $result['result'] = true;
            $data['message'] = lang('add_success');
            $data['xianshi_goods'] = $xianshigoods_info;
            // 自动发布动态
            // goods_id,store_id,goods_name,goods_image,goods_price,goods_freight,xianshi_price
            $data_array = array();
            $data_array['goods_id'] = $goods_info['goods_id'];
            $data_array['store_id'] = session('store_id');
            $data_array['goods_name'] = $goods_info['goods_name'];
            $data_array['goods_image'] = $goods_info['goods_image'];
            $data_array['goods_price'] = $goods_info['goods_price'];
            $data_array['goods_freight'] = $goods_info['goods_freight'];
            $data_array['xianshigoods_price'] = $new_price;
            $this->storeAutoShare($data_array, 'xianshi');

            // 添加任务计划  修改时间
            $this->addcron(array('type' => 2, 'exeid' => $goods_info['goods_id'], 'exetime' => $param['xianshigoods_starttime']));
        }
        else {
            $data['result'] = false;
            $data['message'] = lang('param_error');
        }
        echo json_encode($data);
        die;
    }

    /**
     * 限时折扣商品价格修改
     **/
    public function xianshi_goods_price_edit()
    {
        $xianshigoods_id = intval(input('post.xianshigoods_id'));
        $xianshi_price = floatval(input('post.xianshi_price'));

        $data = array();
        $data['result'] = true;

        $xianshigoods_model = model('pxianshigoods');

        $xianshigoods_info = $xianshigoods_model->getXianshigoodsInfoByID($xianshigoods_id, session('store_id'));
        if (!$xianshigoods_info) {
            $data['result'] = false;
            $data['message'] = lang('param_error');
            echo json_encode($data);
            die;
        }

        $update = array();
        $update['xianshigoods_price'] = $xianshi_price;
        $condition = array();
        $condition['xianshigoods_id'] = $xianshigoods_id;
        $result = $xianshigoods_model->editXianshigoods($update, $condition);

        if ($result) {
            $xianshigoods_info['xianshigoods_price'] = $xianshi_price;
            $xianshigoods_info = $xianshigoods_model->getXianshigoodsExtendInfo($xianshigoods_info);
            $data['xianshi_price'] = $xianshigoods_info['xianshigoods_price'];
            $data['xianshi_discount'] = $xianshigoods_info['xianshi_discount'];

            // 添加对列修改商品促销价格
            \mall\queue\QueueClient::push('updateGoodsPromotionPriceByGoodsId', $xianshigoods_info['goods_id']);

            $this->recordSellerlog(lang('limited_time_discount_price_modified') . $xianshigoods_info['xianshigoods_price'] . '，'.lang('goods_name').'：' . $xianshigoods_info['goods_name']);
        }
        else {
            $data['result'] = false;
            $data['message'] = lang('ds_common_op_succ');
        }
        echo json_encode($data);
        die;
    }

    /**
     * 限时折扣商品删除
     **/
    public function xianshi_goods_delete()
    {
        $xianshigoods_model = model('pxianshigoods');
        $pxianshi_model = model('pxianshi');

        $data = array();
        $data['result'] = true;

        $xianshigoods_id = intval(input('post.xianshigoods_id'));


        $xianshigoods_info = $xianshigoods_model->getXianshigoodsInfoByID($xianshigoods_id);

        if (!$xianshigoods_info) {
            $data['result'] = false;
            $data['message'] = lang('param_error');
            echo json_encode($data);
            die;
        }

        $xianshi_info = $pxianshi_model->getXianshiInfoByID($xianshigoods_info['xianshi_id'], session('store_id'));

        if (!$xianshi_info) {
            $data['result'] = false;
            $data['message'] = lang('param_error');
            echo json_encode($data);
            die;
        }

        if (!$xianshigoods_model->delXianshigoods(array('xianshigoods_id' => $xianshigoods_id))) {
            $data['result'] = false;
            $data['message'] = lang('xianshi_goods_delete_fail');
            echo json_encode($data);
            die;
        }

        //将购物车中的商品全部失效
        $cart_info = db('cart')->where(['goods_id'=>$xianshigoods_info['goods_id']])->select();
        if(!empty($cart_info)){
            foreach ($cart_info as $key => $value) {
                db('cart')->where(['goods_id'=>$value['goods_id']])->update(['status'=>1]);
            }
        }

        // 添加对列修改商品促销价格
        \mall\queue\QueueClient::push('updateGoodsPromotionPriceByGoodsId', $xianshigoods_info['goods_id']);

//        $this->recordSellerlog(lang('delete_time_limited_discount_items') . $xianshi_info['xianshi_name'] . '，'.lang('goods_name').'：' . $xianshigoods_info['goods_name']);
        echo json_encode($data);
        die;
    }

    /**
     * 限时折扣活动取消
     **/
    public function xianshi_cancel()
    {
        $xianshi_id = intval(input('param.xianshi_id'));
        $xianshi_model = model('pxianshi');
        $result = $xianshi_model->cancelXianshi(array('xianshi_id' => $xianshi_id));
        if ($result) {
            $this->log('取消限时折扣活动，活动编号' . $xianshi_id);
            ds_json_encode(10000, lang('ds_common_op_succ'));
        }
        else {
            ds_json_encode(10001, lang('ds_common_op_fail'));
        }
    }


    /**
     * 活动详细信息
     **/
    public function xianshi_detail()
    {
        $xianshi_id = intval(input('param.xianshi_id'));

        $xianshi_model = model('pxianshi');
        $xianshigoods_model = model('pxianshigoods');

        $xianshi_info = $xianshi_model->getXianshiInfoByID($xianshi_id);
        if (empty($xianshi_info)) {
            $this->error(lang('param_error'));
        }
        $this->assign('xianshi_info', $xianshi_info);

        //获取限时折扣商品列表
        $condition = array();
        $condition['xianshi_id'] = $xianshi_id;
        $xianshigoods_list = $xianshigoods_model->getXianshigoodsExtendList($condition,5);
        $this->assign('xianshigoods_list', $xianshigoods_list);
        $this->assign('show_page',$xianshigoods_model->page_info->render());
        return $this->fetch();
    }

    /**
     * 套餐管理
     **/
    public function xianshi_quota()
    {
        $xianshiquota_model = model('pxianshiquota');

        $condition = array();
        $condition['store_name'] = array('like', '%' . input('param.store_name') . '%');
        $xianshiquota_list = $xianshiquota_model->getXianshiquotaList($condition, 10, 'xianshiquota_endtime desc');
        $this->assign('xianshiquota_list', $xianshiquota_list);
        $this->assign('show_page', $xianshiquota_model->page_info->render());

        $this->setAdminCurItem('xianshi_quota');
        return $this->fetch();

    }

    /**
     * 设置
     **/
   public function xianshi_setting() {
        if (!(request()->isPost())) {
            $setting = rkcache('config', true);
            $this->assign('setting', $setting);
            return $this->fetch();
        } else {
            $promotion_xianshi_price = intval(input('post.promotion_xianshi_price'));
            if ($promotion_xianshi_price < 0) {
                $this->error(lang('param_error'));
            }

            $config_model = model('config');
            $update_array = array();
            $update_array['promotion_xianshi_price'] = $promotion_xianshi_price;

            $result = $config_model->editConfig($update_array);
            if ($result) {
                $this->log('修改限时折扣价格为' . $promotion_xianshi_price . '元');
                dsLayerOpenSuccess(lang('setting_save_success'));
            } else {
                $this->error(lang('setting_save_fail'));
            }
        }
    }

    /**
     * ajax修改抢购信息
     */
    public function ajax()
    {
        $result = true;
        $update_array = array();
        $where_array = array();

        switch (input('param.branch')) {
            case 'recommend':
                $pxianshigoods_model = model('pxianshigoods');
                $update_array['xianshigoods_recommend'] = input('param.value');
                $where_array['xianshigoods_id'] = input('param.id');
                $result = $pxianshigoods_model->editXianshigoods($update_array, $where_array);
                break;
        }

        if ($result) {
            echo 'true';
            exit;
        }
        else {
            echo 'false';
            exit;
        }

    }


    /*
     * 发送消息
     */
    private function send_message($member_id, $member_name, $message)
    {
        $param = array();
        $param['from_member_id'] = 0;
        $param['member_id'] = $member_id;
        $param['to_member_name'] = $member_name;
        $param['message_type'] = '1';//表示为系统消息
        $param['msg_content'] = $message;
        $message_model = model('message');
        return $message_model->addMessage($param);
    }
    public function storeAutoShare($data, $type)
    {
        $param = array(
            3 => 'new',
            4 => 'coupon',
            5 => 'xianshi',
            6 => 'mansong',
            7 => 'bundling',
            8 => 'groupbuy'
        );
        $param_flip = array_flip($param);
        if (!in_array($type, $param) || empty($data)) {
            return false;
        }

        $auto_setting = model('storesnssetting')->getStoresnssettingInfo(array('storesnsset_storeid' => session('store_id')));
        $auto_sign = false; // 自动发布开启标志

        if ($auto_setting['storesnsset_' . $type] == 1) {
            $auto_sign = true;

            $goodsdata = addslashes(json_encode($data));
            if ($auto_setting['storesnsset_' . $type . 'title'] != '') {
                $title = $auto_setting['storesnsset_' . $type . 'title'];
            } else {
                $auto_title = 'ds_store_auto_share_' . $type . rand(1, 5);
                $title = lang($auto_title);
            }
        }
        if ($auto_sign) {
            // 插入数据
            $stracelog_array = array();
            $stracelog_array['stracelog_storeid'] = $this->store_info['store_id'];
            $stracelog_array['stracelog_storename'] = $this->store_info['store_name'];
            $stracelog_array['stracelog_storelogo'] = empty($this->store_info['store_avatar']) ? '' : $this->store_info['store_avatar'];
            $stracelog_array['stracelog_title'] = $title;
            $stracelog_array['stracelog_content'] = '';
            $stracelog_array['stracelog_time'] = TIMESTAMP;
            $stracelog_array['stracelog_type'] = $param_flip[$type];
            $stracelog_array['stracelog_goodsdata'] = $goodsdata;
            model('storesnstracelog')->addStoresnstracelog($stracelog_array);
            return true;
        } else {
            return false;
        }
    }
    /**
     * 页面内导航菜单
     *
     * @param string $menu_key 当前导航的menu_key
     * @param array $array 附加菜单
     * @return
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'xianshi_list', 'text' => lang('xianshi_list'), 'url' => url('Promotionxianshi/index')
            ),
            //array(
                //'name' => 'xianshi_quota', 'text' => lang('xianshi_quota'),
                //'url' => url('Promotionxianshi/xianshi_quota')
            //), array(
                //'name' => 'xianshi_setting',
                //'text' => lang('xianshi_setting'),
                //'url' => "javascript:dsLayerOpen('".url('Promotionxianshi/xianshi_setting')."','".lang('xianshi_setting')."')"
            //),
            array(
                'name' => 'xianshi_add',
                'text' => '添加折扣',
                'url' => url('Promotionxianshi/xianshi_add')
            ),
//            array(
//                'name' => 'xianshi_manage',
//                'text' => '商品管理',
//                'url' => url('Promotionxianshi/xianshi_manage')
//            ),
        );
        if (request()->action() == 'xianshi_detail')
            $menu_array[] = array(
                'name' => 'xianshi_detail', 'text' => lang('xianshi_detail'),
                'url' => url('Promotionxianshi/xianshi_detail')
            );

        if (request()->action() == 'xianshi_edit') {
            $menu_array[] = array(
                'name' => 'xianshi_add',
                'text' => '编辑折扣',
                'url' => url('Promotionxianshi/xianshi_add')
            );
            unset($menu_array[1]);
        }
        if (request()->action() === 'xianshi_manage' ) {
            $menu_array[] = array(
                'name' => 'xianshi_manage',
                'text' => '折扣商品管理',
                'url' => url('Promotionxianshi/xianshi_manage', ['xianshi_id' => $_GET['xianshi_id']])
            );
        }

        return $menu_array;
    }
}
